package com.bkgy.modular.qualityCheckRecord.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.qualityCheckRecord.entity.QualityCheckRecord;
import com.bkgy.modular.qualityCheckRecord.mapper.QualityCheckRecordMapper;
import com.bkgy.modular.qualityCheckRecord.service.QualityCheckRecordService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @ClassName QualityCheckRecordMapperServiceImpl
 * @date 2023/5/29 13:49
 */
@Service
public class QualityCheckRecordServiceImpl extends ServiceImpl<QualityCheckRecordMapper, QualityCheckRecord> implements QualityCheckRecordService {

    @Autowired
    private QualityCheckRecordMapper qualityCheckRecordMapper;

    @Override
    public ResultData findByData(JSONObject jsonObject) {
        // 接收分页参数
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");

        // 判断分页参数是否存在
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }

        // 接收查询条件参数
        JSONObject condition = jsonObject.getJSONObject("condition");

        List<QualityCheckRecord> list = qualityCheckRecordMapper.findByData((pageIndex - 1) * pageSize, pageSize * pageIndex, condition);
        Integer count = qualityCheckRecordMapper.findByDataCount(condition);
        return ResultData.ResultDataSuccess(list,count);
    }

    @Override
    public ResultData updateData(JSONObject jsonObject) {
        String extid = jsonObject.getString("extid");
        if (!StringUtils.hasText(extid)) {
            return ResultData.ResultDataFaultSelf("参数有误",-1);
        }
        // 接收参数
        JSONObject condition = jsonObject.getJSONObject("condition");
        qualityCheckRecordMapper.updateData(extid,condition);
        return ResultData.ResultDataFaultSelf("操作成功",1);
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        // 创建表格数据集合
        List<QualityCheckRecord> listExcel = new ArrayList<>();

        // 调用service查询出数据
        List<QualityCheckRecord> data = null;
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = qualityCheckRecordMapper.findByData((i - 1) * pageSize, i * pageSize, condition);
            for (QualityCheckRecord item : data) {
                listExcel.add(item);
            }
        }
        ExcelUtils.downloadExcel(fileNames,response, QualityCheckRecord.class,listExcel);
    }
}
